package com.cloudtasks;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class GroceryGroupsEditActivity extends Activity {
	
	String UserName = "";
	long UserID;
	String ListName = "";
	long ListID;
	String TypeName = "";
	final Context myContext = this;
	private Cursor DBcursor;
	final static String TYPE_PAGE = "item";
	private Database mDb;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_list_edit_itempage);
        
        try{        
        	// Add Button RETURN
	        Button addItem = (Button)findViewById(R.id.return_item_edit_button);
	        addItem.setOnClickListener(returnListener);  
        	
	        // Unwrap parameters from last activity: the User's name
	        // and also his User ID
        	Bundle param = getIntent().getExtras(); 
	        if(param != null){
	        	UserName = param.getString("uname");
		        UserID = param.getLong("uid");
		        ListName = param.getString("");
		    	ListID = param.getLong("lid");
		    	//TypeName = param.getString("tname");
	        }
        	 
	        // initialize the Database for this context
	        mDb = new Database(this);
	        mDb.open();
	        ListView lv = (ListView) findViewById(R.id.items_edit_listview);
	        DBcursor = mDb.fetchAllItemsInList(ListID);
	        startManagingCursor(DBcursor);
	        //DBcursor = mDb.fetchAllItemsInListByType(ListID, "Fruit");
	        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,  
	        	    android.R.layout.simple_list_item_1,
	        	    DBcursor,
	        	    new String[] {Database.KEY_GROC_NAME},
	        	    new int[] { android.R.id.text1}
	        	);
	        lv.setAdapter(cursorAdapter);
	        
	        
	     // what happens when you click one entry in the ListView
	        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
				@Override
				public void onItemClick(AdapterView<?> av, View view,
						int pos, long id) {
					CustomDialog editPopup = new CustomDialog(OptionTabsInitializer.context, mDb, DBcursor, "edit", TYPE_PAGE);
	        		ListView lv = (ListView) findViewById(R.id.items_edit_listview);	        		
	        		editPopup.passNameID(lv.getAdapter().getItemId(pos));
	        		editPopup.show();
				}  
	        });  
        }
        catch(Exception e){
        	MyTools.displayToastMessage(myContext, e.toString());
        }
    }
    
    /////////////// CLICK LISTENERS /////////////////
    /* Add Listener */
    private OnClickListener returnListener = new OnClickListener(){
		public void onClick(View v){		
			
		/*	try {
				Intent intent = new Intent(myContext,
						GroceryGroupsDisplayPage1Activity.class);
				intent.putExtras(getIntent().getExtras());
				TabGroupActivity parentActivity = (TabGroupActivity) getParent();
				
				startActivity(parentActivity.getIntent());
			} catch (Exception e) {
				MyTools.displayToastMessage(myContext, e.toString());
			}*/
			//finish();
			onBackPressed();
			//back();
		}
	};
    
    @Override
    public synchronized void onResume() {
        super.onResume();
       // DBcursor.requery();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mDb.close();
    }
}
